using System;
using System.Windows.Forms;
using Sharpmark.PerfectLife.Core.HtmlDocument;

namespace Sharpmark.PerfectLife.BlogAssist
{
    public partial class FrmMain : Form
    {
        private string _originalImageSize = null;
        public FrmMain()
        {
            InitializeComponent();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (txtImageUrl.Text != "")
            {
                ImageAlignType align = ImageAlignType.NotAlign;
                if (rbnLeftAlign.Checked) align = ImageAlignType.LeftAlign;
                else if (rbnRightAlign.Checked) align = ImageAlignType.RightAlign;

                string html = BlogHtmlGenerator.CreateLightBoxImageHtml(
                    txtImageUrl.Text, txtImageTitle.Text, txtSubject.Text,
                    ckbOpenNewWindows.Checked,
                    Int32.Parse(nudWidth.Value.ToString()),
                    Int32.Parse(nudHeight.Value.ToString()),
                    align)
                    + System.Environment.NewLine;
                if (ckbAppendHtmlCode.Checked)
                {
                    txtHtml.Text += html;
                }
                else
                {
                    txtHtml.Text = html;
                }
            }
            else
            {
                // should display some messages to notify the user.
            }
        }

        private void TextBox_Enter(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            txt.SelectAll();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            Text = ProductName + " Ver" + this.ProductVersion;
            cbbOriginal.SelectedItem = cbbOriginal.Items[3];
        }

        private void TextBox_MouseEnter(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            txt.Focus();
        }

        private void btnGetFromClipboard_Click(object sender, EventArgs e)
        {
            txtImageUrl.Text = BlogHtmlGenerator.SplitImageUrl(Clipboard.GetText());
        }

        private void FrmMain_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetFormats()[0] == "System.String")
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
        private void FrmMain_DragDrop(object sender, DragEventArgs e)
        {
            txtImageUrl.Text = BlogHtmlGenerator.SplitImageUrl(
                (string)e.Data.GetData(DataFormats.Text));
        }

        private void cbbOriginal_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtImageUrl.Text = txtImageUrl.Text.Replace(
                "s" + _originalImageSize, "s" + cbbOriginal.Text);
        }

        private void txtImageUrl_TextChanged(object sender, EventArgs e)
        {
            if (txtImageUrl.Text.Contains("s144"))
            {
                cbbOriginal.Text = _originalImageSize = "144";
            }
            else if (txtImageUrl.Text.Contains("s288"))
            {
                cbbOriginal.Text = _originalImageSize = "288";
            }
            else if (txtImageUrl.Text.Contains("s400"))
            {
                cbbOriginal.Text = _originalImageSize = "400";
            }
            else if (txtImageUrl.Text.Contains("s800"))
            {
                cbbOriginal.Text = _originalImageSize = "800";
            }
        }
    }
}